//初始化FastClick的参数
$(function () {
    FastClick.attach(document.body);
});

//获取发分类的分类数据，并且用template模板渲染到页面中
;$(function () {
    $.ajax({
        type: "get",
        url: "http://193.112.55.79:9090/api/getcategorytitle",
        dateType: "json",
        success:function (res) {          
            var htmlStr = template("wareComtentsTmp",res);
            $("#accordion_wareComtents").html(htmlStr);
        }
    });
});

//初始化id=accordion_wareComtents折叠菜单（必须初始化）
bui.ready(function () {

    //折叠菜单示例
    var uiAccordion = bui.accordion({
        id: "#accordion_wareComtents"
    });

    //设置点击菜单后 下拉菜单显示的速度
    // uiAccordion.on("show", function () {
    //     // 点击的菜单
    //     console.log(this);
    // });

});

//初始化id=accordion_wareComtents（必须初始化）
bui.ready(function () {
    var uiAccordion = bui.accordion({
        id: "#accordion_wareComtents"
    });
})

//获取大分类下的具体数据，然后渲染到相应的大分类界面下
$("#accordion_wareComtents").on("click","dt",function(){   
    //获取大分类的id，以便作为插入数据的时候用
    var refineId = $(this).attr("data-tittleId");
    var insertDd = $(this).parent().children('*:nth-child(2n)').eq(refineId);
    var liStr = "<a class='bui-box-center bui-left'></a>";

    $.ajax({
        type: "get",
        url: "http://193.112.55.79:9090/api/getcategory?titleid=" + refineId,
        dateType: "json",
        success: function(res) {
            var htmlStr = template("wareComtentsRefineTmp", res); 
            var num = res.result.length;
            if (num % 3 == 1) {
                htmlStr += liStr;
                htmlStr += liStr;
            }
            if (num % 3 == 2) {
                htmlStr += liStr;
            }
            insertDd.html(htmlStr);            
        }
    });    
})

//设置iscroll的初始化
$(function(){
    var myScroll = new IScroll("#mmm_wareComtents", {
        mouseWheel: true,
        scrollbars: true
    });
})


//点击获取页面参数跳转
bui.ready(function () {

    // 动态绑定
    $("#accordion_wareComtents").on("click", "dd a", function(argument) {
        var categoryId = $(this).attr("data-categoryId"),
            category = $(this).attr("data-category"),
            titleId = $(this).attr("data-titleId");

        var urlStr = "http://193.112.55.79:9090/api/getproductlist?categoryid=" + categoryId+"&pageid=1";
        
        bui.load({
            url: "./categoryList.html",
            param: { urlStr: urlStr }
        });
    });
})

